# 继承
# python2有经典类（不继承object）和新式类（继承object）
# python3所有类默认继承object，要兼容python2可以显示继承object

class Parent1:
    x = 1


class Parent2:
    pass


class Sub1(Parent1):  # 单继承
    pass


class Sub2(Parent1, Parent2):  # 多继承
    pass


class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age


class Worker(Person):
    # 扩展父类功能
    def __init__(self, name, age, sex, salary):
        Person.__init__(self, name, age)
        self.sex = sex
        self.salary = salary


worker = Worker('tom', 18, 'male', 5000)
print(worker.__dict__)

# 单继承 属性查找 先从自己找，再从父类找
print(Worker.mro())  # 继承属性查找列表


# 菱形继承
class A:
    x = 1
    pass


class B(A):
    x = 1
    pass


class C(A):
    x = 1
    pass


class D(B, C):
    x = 1
    pass


print(D.mro())  # 属性查找顺序按mro列表


# 如果多继承是菱形继承，经典类深度优先，新式类广度优先

# 多继承优化： mixins机制

class Vehicle:
    pass


# Mixins类一般以Mixin，able或者ible为后缀
# Mixins类代表一种功能，责任单一，有多个功能必须用多个Mixin类实现
class FlyableMixin:
    def fly(self):
        pass


class CivilAircraft(FlyableMixin, Vehicle):
    pass


class Helicopter(FlyableMixin, Vehicle):
    pass


class Car(Vehicle):
    pass


# super() 调用父类方法
class People:
    def __init__(self, name):
        self.name = name


class Teacher:
    def __init__(self, name, age):
        # super会传入当前类和对象，生成一个特殊对象，从mro()父类顺序开始查找方法
        # super(Teacher, self).__init__(name)
        super().__init__(name)
        self.age = age


# super 案例
class A1:
    def test(self):
        print('from A1')
        # super会得到一个特殊对象
        # 会从属性查找发起者的mro开始查找父类的方法
        super(A1, self).test()


class B1:
    def test(self):
        print('from B1')


class C1(A1, B1):
    pass

c=C1()
c.test()
